﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BrnMall.Manager.Domain.WeApp;

namespace BrnMall.Manager.Manager.WeApp
{
    /// <summary>
    /// 小程序授权信息
    /// </summary>
    public static class AppAuthorizationManager
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static AppAuthorization GetAppAuthorization(int id)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<AppAuthorization>(id);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="appId"></param>
        /// <returns></returns>
        public static AppAuthorization GetAppAuthorizationByAppId(string appId)
        {
            if (string.IsNullOrEmpty(appId)) return null;
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<AppAuthorization>("select top 1 * from weapp_authorization where authorizer_appid=@0",appId);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="appId"></param>
        /// <returns></returns>
        public static AppAuthorization GetAppAuthorizationByStoreAppId(int storeAppId)
        {
           
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<AppAuthorization>("select top 1 a.* from weapp_authorization a left join weapp_storeapp b on a.id=b.authorizationId where b.storeappid=@0", storeAppId);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="auth"></param>
        public static void AddAuth(AppAuthorization auth)
        {
            using (var db = new QDMallDatabase())
            {
                db.Insert(auth);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="auth"></param>
        public static void UpdateAuth(AppAuthorization auth)
        {
            using (var db = new QDMallDatabase())
            {
                db.Update(auth);
            }
        }
        /// <summary>
        /// 删除授权
        /// </summary>
        /// <param name="id"></param>
        public static void DeleteAuth(int id)
        {
            var storeapp = StoreApplicationManager.GetStoreApplicationByAuthId(id);
            if (storeapp == null) return;
            using (var db = new QDMallDatabase())
            {
                db.Delete<AppAuthorization>(id);
                //db.Execute("update weapp_storeapp set authorizationId=0,devstatus=1,AuditId=0,AuditInfo='',templateid=@1 where authorizationId=@0", id,weapp.TemplateId);
                db.Execute("update weapp_storeapp set authorizationId=0 where authorizationId=@0", id);
            }
        }

        /// <summary>
        /// 刷新令牌
        /// </summary>
        /// <param name="authorizerAppid"></param>
        /// <param name="authorizerAccessToken"></param>
        /// <param name="expiresIn"></param>
        /// <param name="authorizerRefreshToken"></param>
        public static void UpdateAuthorizerAccessToken(string authorizerAppid, string authorizerAccessToken,
            DateTime expiresIn,string authorizerRefreshToken)
        {
            using (var db = new QDMallDatabase())
            {
                db.Execute(
                    "update weapp_authorization set authorizer_access_token=@0,expires_in=@1,authorizer_refresh_token=@2 where authorizer_appid=@3",
                    authorizerAccessToken, expiresIn, authorizerRefreshToken, authorizerAppid);
            }
        }
    }
}
